﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;

namespace MxWeiXinPF.Web
{
    public partial class register : Page
    {
        private MxWeiXinPF.BLL.wx_dt_user_BaseData daladd = new MxWeiXinPF.BLL.wx_dt_user_BaseData();
        private string popenid = "";
        private int pwid = 0;

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string str = "";
            string url = "";
            int id = 0;
            if (this.txtBirthday.Text.Trim().Equals(""))
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"生日不能为空\");</script>");
            }
            else
            {
                id = this.DoAdd();
                if (id > 0)
                {
                    this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"保存成功，即将回到您之前访问的页面！\");</script>");
                    MxWeiXinPF.Model.users model = new MxWeiXinPF.BLL.users().GetModel(id);
                    if (model != null)
                    {
                        this.Session["dt_session_user_info"] = model;
                        this.Session.Timeout = 0x2d;
                        Utils.WriteCookie("DLUserName", model.user_name);
                        Utils.WriteCookie("dt_cookie_user_name_remember", model.user_name);
                        Utils.WriteCookie("dt_cookie_user_pwd_remember", model.password);
                        if (base.Request["ReturnURL"] != null)
                        {
                            str = base.Request["ReturnURL"].ToString();
                        }
                        url = MyCommFun.urlAddOpenid(str.Replace("openid", "popenid"), model.wxOpenId);
                        HttpContext.Current.Response.Redirect(url);
                    }
                }
                else
                {
                    this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"保存失败，请联系管理员！\");</script>");
                }
            }
        }

        private int DoAdd()
        {
            DateTime time;
            MxWeiXinPF.Model.users model = new MxWeiXinPF.Model.users();
            MxWeiXinPF.BLL.users users2 = new MxWeiXinPF.BLL.users();
            if (users2.Exists(this.txtUserName.Text.Trim()))
            {
                return 0;
            }
            MxWeiXinPF.Model.users modelByOpenID = this.daladd.GetModelByOpenID(this.popenid);
            if (modelByOpenID != null)
            {
                model.parentopenid = modelByOpenID.id.ToString();
                model.shopid = modelByOpenID.shopid;
            }
            model.wid = modelByOpenID.wid;
            model.user_name = Utils.DropHTML(this.txtUserName.Text.Trim());
            model.salt = Utils.GetCheckCode(6);
            model.password = DESEncrypt.Encrypt(this.txtPassword.Text.Trim(), model.salt);
            model.email = Utils.DropHTML(this.txtEmail.Text);
            model.nick_name = Utils.DropHTML(this.txtNickName.Text);
            model.avatar = Utils.DropHTML(this.txtAvatar.Text);
            model.sex = this.rblSex.SelectedValue;
            if (DateTime.TryParse(this.txtBirthday.Text.Trim(), out time))
            {
                model.birthday = time;
            }
            model.telphone = Utils.DropHTML(this.txtMobile.Text.Trim());
            model.mobile = Utils.DropHTML(this.txtMobile.Text.Trim());
            model.address = Utils.DropHTML(this.txtAddress.Text.Trim());
            model.truename = Utils.DropHTML(this.txttruename.Text.Trim());
            model.jobtype = Utils.DropHTML(this.txtjobtype.Text.Trim());
            model.reg_time = DateTime.Now;
            model.reg_ip = MXRequest.GetIP();
            return users2.Add(model);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack && (base.Request["ReturnURL"] != null))
            {
                string str = base.Request["ReturnURL"].ToString().Split(new char[] { '&' })[1];
                this.popenid = str.Split(new char[] { '=' })[1];
                string str2 = base.Request["ReturnURL"].ToString().Split(new char[] { '?' })[1].Split(new char[] { '&' })[0];
                this.pwid = int.Parse(str2.Split(new char[] { '=' })[1]);
            }
        }
    }
}